package com.sinaapp.gavinzhang.GSpider.http;

import com.sinaapp.gavinzhang.GSpider.http.annotation.ServletClass;
import com.sinaapp.gavinzhang.GSpider.http.annotation.ServletMethod;

import javax.servlet.http.HttpServlet;
import java.io.File;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gavin on 2015-11-22.
 */
public class ServletMapping {
    private static List<Class<?>> list;
    static {
        list = new ArrayList<>();
        for (Class<?> c : Tools.getClasses("com.sinaapp.gavinzhang.GSpider.http.servlet")) {
            ServletClass servletClass = c.getAnnotation(ServletClass.class);
            if (servletClass != null)
            {
                list.add(c);
            }
        }
    }

    private HttpServlet httpServlet;
    private Class<?> aClass;
    public ServletMapping(String request) {
        //List<Class<?>> list = Tools.getClasses("com.sinaapp.gavinzhang.GSpider.http.servlet");
        for (Class<?> c : list)
        {
            ServletClass servletClass = c.getAnnotation(ServletClass.class);
            if (servletClass != null)
            {
                if (servletClass.requestName().equals(request))
                {
                    aClass = c;
                    break;
                }
            }
        }

    }

    public HttpServlet getServlet()
    {
        if (aClass != null)
        {
            try {
                httpServlet = (HttpServlet) aClass.newInstance();
                return httpServlet;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args)
    {
        ServletMapping servletMapping = new ServletMapping("/search");

    }
}
